package com.cloud.lawcase.api.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cloud.lawcase.common.driver.SimpleInlangDriver;
import com.cloud.lawcase.common.openapi.OpenApiLawBean;

import org.apache.ibatis.annotations.*;

import com.cloud.lawcase.api.bean.LawCriteriaBean;
import com.cloud.lawcase.api.bean.LawSearchCriteria;
import com.cloud.lawcase.api.entity.TcDicLaw;
import com.cloud.lawcase.api.provider.ExotericLawPageProvider;
import com.cloud.lawcase.api.provider.TcDicLawPageProvider;
import com.cloud.lawcase.common.utils.BaseMapperHelper;

public interface TcDicLawMapper extends BaseMapperHelper<TcDicLaw> {

    @SelectProvider(type = TcDicLawPageProvider.class, method = "listPage")
    @Results({@Result(property = "lawId", column = "law_id", javaType = Long.class),
            @Result(property = "lawCode", column = "law_code", javaType = String.class),
            @Result(property = "lawTitle", column = "law_title", javaType = String.class),
            @Result(property = "lawDesc", column = "law_desc", javaType = String.class),
            @Result(property = "publishDocCode", column = "publish_doc_code", javaType = String.class),
            @Result(property = "approvalDate", column = "approval_date", javaType = Date.class),
            @Result(property = "publishDate", column = "publish_date", javaType = Date.class),
            @Result(property = "releaseDate", column = "release_date", javaType = Date.class),
            @Result(property = "publishDeptNames", column = "publish_dept_names", javaType = String.class),
            @Result(property = "approvalDeptNames", column = "approval_dept_names", javaType = String.class),
            @Result(property = "sxName", column = "sx_name", javaType = String.class),
            @Result(property = "maxChapterId", column = "max_chapter_id", javaType = Integer.class),
            @Result(property = "maxSectionId", column = "max_section_id", javaType = Integer.class),
            @Result(property = "maxArticleId", column = "max_article_id", javaType = Integer.class),
            @Result(property = "lawTypeName", column = "law_type_name", javaType = String.class),
            @Result(property = "effectLevelName", column = "effect_level_name", javaType = String.class),
            @Result(property = "groupId", column = "group_id", javaType = Long.class),
            @Result(property = "groupName", column = "group_name", javaType = String.class)})
    List<TcDicLaw> listPage(LawCriteriaBean criteria);

    @Delete("delete from tc_dic_law where group_id = #{groupId}")
    void deleteByGroupId(@Param("groupId") Long groupId);

    @Select("SELECT sum(case WHEN sx_id = 1 then count else 0 end)  as itemOne, sum(case WHEN sx_id = 2 then count else 0 end) as itemTwo, sum(case WHEN sx_id = 3 then count else 0 end) as itemThree,\n"
            + "sum(case WHEN (sx_id != -1 or sx_id is null) then count else 0 end) as allCount from (\n"
            + "SELECT sx_id,count(*) as count from tc_dic_law where group_id = #{id} GROUP BY  sx_id ) t")
    @Lang(SimpleInlangDriver.class)
    @ResultType(Map.class)
    List<Map<String, Object>> statLawByGroupId(@Param("id") Long id);

    @Select("SELECT sum(case WHEN sx_id = 1 then count else 0 end)  as itemOne, sum(case WHEN sx_id = 2 then count else 0 end) as itemTwo, sum(case WHEN sx_id = 3 then count else 0 end) as itemThree,\n"
            + "sum(case WHEN (sx_id != -1 or sx_id is null) then count else 0 end) as allCount from (\n"
            + "SELECT sx_id,count(*) as count from tc_dic_law GROUP BY  sx_id ) t")
    @Lang(SimpleInlangDriver.class)
    @ResultType(Map.class)
    List<Map<String, Object>> statLaw();

    @Select("SELECT law_id,b.excute_type_name,count from (\n"
            + "SELECT law_id,excute_type_id, count(*) as count from tc_dic_law_cause GROUP BY law_id,excute_type_id) a LEFT JOIN \n"
            + "tc_dic_law_excute_type b on a.excute_type_id = b.excute_type_id WHERE law_id = #{id}")
    @Lang(SimpleInlangDriver.class)
    @ResultType(Map.class)
    List<Map<String, Object>> lawExecuteTypeStat(Long id);

    @SelectProvider(type = ExotericLawPageProvider.class, method = "listPage")
    @Results({@Result(property = "lawCode", column = "law_code", javaType = String.class),
            @Result(property = "lawTitle", column = "law_title", javaType = String.class),
            @Result(property = "lawID", column = "law_id", javaType = Integer.class),
            @Result(property = "lawDesc", column = "law_desc", javaType = String.class),
            @Result(property = "publishDocCode", column = "publish_doc_code", javaType = String.class),
            @Result(property = "approvalDate", column = "approval_date", javaType = Date.class),
            @Result(property = "publishDate", column = "publish_date", javaType = Date.class),
            @Result(property = "publishDeptNames", column = "publish_dept_names", javaType = String.class),
            @Result(property = "approvalDeptNames", column = "approval_dept_names", javaType = String.class),
            @Result(property = "approvalDeptNames", column = "approval_dept_names", javaType = String.class),
            @Result(property = "lawTypeName", column = "law_type_name", javaType = String.class),
            @Result(property = "regionName", column = "region_name", javaType = String.class),
            @Result(property = "groupCode", column = "group_code", javaType = String.class)})
    List<OpenApiLawBean> searchExotericLaw(LawSearchCriteria criteria);

    @Select("select law_id,law_title from tc_dic_law where law_code = #{lawCode}")
    @Results({@Result(property = "lawTitle", column = "law_title", javaType = String.class),
            @Result(property = "lawId", column = "law_id", javaType = Long.class)})
    List<TcDicLaw> getDetail(@Param("lawCode") String code);
}
